// 加法
export function numberAdd(arg1, arg2) {
	if ((!arg1 || !arg2) && arg1 != 0 && arg2 != 0) {
		console.log('Error: 加法需要传入2个数字')
		return
	}
	let arg1Length = 0 // a的小数部分长度
	let arg2Length = 0 // b的小数部分长度
	try {
		arg1Length = arg1.toString().split('.')[1].length
	} catch (f) {}
	try {
		arg2Length = arg2.toString().split('.')[1].length
	} catch (f) {}

	let resNum = 10 ** Math.max(arg1Length, arg2Length) // 保证a、b为整数的最小10次幂
	return (arg1 * resNum + arg2 * resNum) / resNum
}

// 减法
export function numberSub(arg1, arg2) {
	if ((!arg1 || !arg2) && arg1 != 0 && arg2 != 0) {
		console.log('Error: 减法运算需要传入2个数字')
		return
	}
	let arg1Length = 0 // a的小数部分长度
	let arg2Length = 0 // b的小数部分长度
	try {
		arg1Length = arg1.toString().split('.')[1].length
	} catch (f) {}
	try {
		arg2Length = arg2.toString().split('.')[1].length
	} catch (f) {}

	let resNum = 10 ** Math.max(arg1Length, arg2Length) //保证a、b为整数的最小10次幂
	return (arg1 * resNum - arg2 * resNum) / resNum
}

// 乘法
export function numberMul(arg1, arg2) {
	if ((!arg1 || !arg2) && arg1 != 0 && arg2 != 0) {
		console.log('Error: 乘法运算需要传入2个数字')
		return
	}
	let arg1Length = 0 // a的小数部分长度
	let arg2Length = 0 // b的小数部分长度
	try {
		arg1Length = arg1.toString().split('.')[1].length
	} catch (f) {}
	try {
		arg2Length = arg2.toString().split('.')[1].length
	} catch (f) {}

	return (Number(arg1.toString().replace('.', '')) * Number(arg2.toString().replace('.', ''))) / (10 ** (arg1Length +
		arg2Length))
}

// 除法
export function numberDiv(arg1, arg2) {
	if ((!arg1 || !arg2) && arg1 != 0) {
		console.log('Error: 减法运算需要传入2个数字')
		return
	}
	let arg1Length = 0 // a的小数部分长度
	let arg2Length = 0 // b的小数部分长度
	try {
		arg1Length = arg1.toString().split('.')[1].length
	} catch (f) {}
	try {
		arg2Length = arg2.toString().split('.')[1].length
	} catch (f) {}

	const molecule = Number(arg1.toString().replace('.', '')) * (10 ** (arg1Length + arg2Length))
	const denominator = Number(arg2.toString().replace('.', '')) * (10 ** (arg1Length + arg2Length))
	return molecule / denominator / (10 ** (arg1Length - arg2Length))
}

//处理三级分类
export const organizeArray = (items) => {
	// 根据tier分类  
	const tier0 = items.filter(item => item.tier === 0);
	const tier1 = items.filter(item => item.tier === 1);
	const tier2 = items.filter(item => item.tier === 2);
	// 初始化一级数组  
	const result = tier0.map(item => ({
		...item,
		children: []
	}));
	//父级id跟子级parentId进行关联
	tier1.forEach(item => {
		const parent = result.find(p => p.id === item.parentId);
		if (parent) {
			parent.children.push({
				...item,
				subchildren: []
			});
		}
	});
	tier2.forEach(item => {
		const parent = result.find(p => p.children.some(c => c.id === item.parentId));
		if (parent) {
			const child = parent.children.find(c => c.id === item.parentId);
			if (child) {
				child.subchildren.push(item);
			}
		}
	});
	return result;
}

export const handlePhone = (phone) => {
	var mobileRegex = /^1[3-9]\d{9}$/;
	if (mobileRegex.test(phone)) {  
	    return true
	} else {  
	    return false 
	}
}